import '@src/util/httpApi';
import '@src/common/entry';
import router from './router';
import App from './App.vue';
import store from './store';

import { Loading } from 'element-ui';
import { getInitAuth } from './initAuth';
/* hooks */
import { useEnv } from '@hooks/useEnv';

import Vue from 'vue'
import DomHints from 'vue-dom-hints'
import VConsole from 'vconsole'

import { useNewVue } from '@hooks/useNewVue'
import { getRootWindow } from 'pub-bbx-utils';
import { isFalsy } from 'pub-bbx-utils';

import 'pub-bbx-pc-vue2/style'

const { createVue } = useNewVue()

/**
 * @description 一个 Vue.js 2 插件，可以使用简单的浏览器开发工具（DOM 检查和控制台）更轻松地检查组件
 * @see https://github.com/privatenumber/vue-dom-hints
 * !! 只在开发环境下使用
*/
try {

  const isProduction = process.env.NODE_ENV === 'production'
  const isDevelopment = !isProduction

  if (isDevelopment) {
    Vue.use(DomHints)
  }

} catch (error) {
  console.error(error)
}

try {
  const rootWindow = getRootWindow()
  const isShowConsole = rootWindow?.isShowConsole
  if (isShowConsole) {
    new VConsole()
  }
} catch (error) {
  console.error(error)
}

let app = createVue({
  // el: '#app',
  render: h => h(App),
  router,
  store,
});

async function init() {
  
  // 是否为开发环境
  const { isDev } = useEnv()
  
  // 如果是生产环境，直接挂载
  if (isFalsy(isDev.value)) {
    // 挂载应用
    mountApp()
    return
  }
  
  try {
    
    const loadingInstance = Loading.service({
      lock: true,
      text: '初始化中... (当前 loading 仅在开发环境下显示)',
      background: 'rgba(0, 0, 0, 0.7)'
    });
    
    // 支持本地（单页面调试）获取权限等信息
    await getInitAuth(); 
    
    // 挂载应用
    mountApp()
    
    // 关闭loading
    loadingInstance.close()
    
  } catch (error) {
    mountApp()
    console.error('getInitAuth error: ', error)
  }
  
}

function mountApp() {
  app.$mount('#app');
}

init();

export default app;
